﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ClassLibrary;
using System.ServiceModel;

namespace TheBestPlayer2016
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            App.MainWindowEl = this;
            InitializeComponent();

            try
            {
                EndpointAddress address = new EndpointAddress("http://localhost:8090/TheBestPlayer");
                App.Client = new Client();
                DuplexChannelFactory<IServerContract> duplexChannel = new DuplexChannelFactory<IServerContract>(App.Client, new WSDualHttpBinding(), address);
                
                App.Server = duplexChannel.CreateChannel();



                App.Matchs = App.Server.GetMatchs();

                dataGrid.ItemsSource = App.Matchs;
            }
            catch(Exception e)
            {
                MessageBox.Show("Server not available !", string.Empty, MessageBoxButton.OK);
                MessageBox.Show(e.Message);
                //this.Close();
            }

        }

        private void goToChoosePlayer_Click(object sender, RoutedEventArgs e)
        {
            Match selectedMatch = (Match)dataGrid.SelectedItem;
            if(selectedMatch != null)
            {
                
                App.ChoosePlayerEl = App.ChoosePlayerEl ?? new ChoosePlayer(selectedMatch);
                App.ChoosePlayerEl.Show();
                App.MainWindowEl.Hide();
            }
            else
            {
                MessageBox.Show("Please choose a game");
            }

            
        }
    }
}
